package com.woniu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Admin;
import com.woniu.pojo.User;
import com.woniu.service.IAdminService;
import com.woniu.service.IUserService;
import com.woniu.vo.UserQueryVo;
import com.woniu.vo.UserWithCarListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户管理")
//@CrossOrigin  // 解决跨域的问题
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IAdminService adminService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> params) {
        //1.获取参数
        String account = params.get("username");
        String password = params.get("password");
        //2.对参数进行非空校验
        if(StringUtils.isEmpty(account)){
            return error();
        }
        if(StringUtils.isEmpty(password)){
            return error();
        }
        //3.调用方法进行登录操作，即是查询数据，如果查到就表明登录成功，如果没查到就表明登录失败
        Admin admin=adminService.login(account,password);
        //4.向前端反馈数据
        if(admin==null){
            return error();
        }
        Map<String,String> map=new HashMap<>();
        map.put("token",account);
        return toDataResult(map);
    }

    @ApiOperation("模拟获取用户信息")
    @GetMapping("/info")
    public Result info(String token) {
        //1.获取参数
        log.debug("进入到获取用户数据的方法====");
        log.debug(token);
        //2.对参数进行非空校验 TODO
        //3.对返回数据进行封装
        Map<String,Object> map=new HashMap<>();
        map.put("name",token);
        map.put("avatar", "https://img1.baidu.com/it/u=2838100141,2488760005&fm=253&app=138" +
                "&size=w931&n=0&f=JPEG&fmt=auto?sec=1663520400&t=7091611ba73832e3566f237c4e9a09ed");
        map.put("roles", new String[]{"admin", "teacher", "student"});
        //4.将数据进行返回
        return toDataResult(map);
    }


    @ApiOperation("模拟退出")
    @PostMapping("/logout")
    public Result logout() {
        log.debug("=====进入退出方法=====");
        return success();
    }

    @PostMapping("//{currentPage}//{pageSize}")
    public Result queryPage(@PathVariable Long currentPage, @PathVariable Long pageSize,
                            @RequestBody UserQueryVo userQueryVo) {
        Page<User> userPage = userService.queryPage(userQueryVo, currentPage, pageSize);
        return toDataResult(new PageResult(userPage.getTotal(), userPage.getRecords()));
    }

    @PostMapping("//withCarListWithOrderList//{currentPage}//{pageSize}")
    public Result queryWithCarListWithOrderListPage(@PathVariable Long currentPage, @PathVariable Long pageSize,
                            @RequestBody UserQueryVo userQueryVo) {
        Page<UserWithCarListVo> userPage = userService.queryWithCarListWithOrderListPage(userQueryVo, currentPage, pageSize);
        return toDataResult(new PageResult(userPage.getTotal(), userPage.getRecords()));
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        boolean result = userService.save(user);
        return toResult(result);
    }

    @PutMapping
    public Result Edit(@RequestBody User user) {
        boolean result = userService.updateById(user);
        return toResult(result);
    }

    @DeleteMapping("//{id}")
    public Result remove(@PathVariable Long id) {
        boolean result = userService.removeById(id);
        return toResult(result);
    }

}
